package com.geline.easytrans.config;

import com.geline.cloud.core.BaseTimedCache;
import com.geline.easytrans.handler.DefaultTransDictHandler;
import com.geline.easytrans.handler.DefaultTransSQLHandler;
import com.geline.easytrans.handler.TransDictHandler;
import com.geline.easytrans.handler.TransSQLHandler;
import com.geline.easytrans.service.EasyTransService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * 数据翻译自动配置(处理：@TransDict,@TransSQL,@TransField)
 *
 * @author: mengxin
 * @date: 2023/9/24 17:27
 */
@Slf4j
public class EasyTransAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public TransDictHandler initTransDictHandler() {
        return new DefaultTransDictHandler();
    }

    @Bean
    @ConditionalOnMissingBean
    public BaseTimedCache initBaseTimedCache(){
        log.info("... init BaseTimedCache ...");
        return new BaseTimedCache();
    }

    //可以自定义，然后加缓存
    @Bean
    @ConditionalOnMissingBean
    public TransSQLHandler initEasyTransDao(JdbcTemplate jdbcTemplate, SqlSessionFactory sqlSessionFactory, BaseTimedCache cache){
        return new DefaultTransSQLHandler(jdbcTemplate, sqlSessionFactory, cache);
    }

    @Bean
    @ConditionalOnMissingBean
    public EasyTransService initEasyTransService(TransDictHandler handler, TransSQLHandler dao){
        return new EasyTransService(handler, dao);
    }
}
